/*
    Project:        Home Power Control
    File:           HPCDHT.h
    Author:         christian@karunamusic.at
    Date:           02.2015
    Description:    Sensor to measure humidity and temperature with the DHT22.
                    Code partly from Adafruit
                    https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated
 */

#ifndef HPCDHT_H
#define HPCDHT_H

#include <HPCSensor.h>

/** \brief Baseclass of the DHT22-sensor to measure the humidity and temperature.
 */
class HPCDHT : public HPCSensor
{
    Q_OBJECT
public:
    /** \brief Constructor
     *
     * \param type type of the sensor.
     * \param name name of the sensor.
     * \param id database id of the sensor.
     * \param pin Inputpin.
     */
    HPCDHT( TSensorType type, const QString& name, quint32 id, quint32 pin );
    /** \brief Destructor.
     */
    virtual ~HPCDHT();

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();

    /** \brief Query the sensor and calculate the result.
     * Set m_Value and m_AddVal with the result.
     *
     * \return bool, true if successful.
     */
    bool querySensor();
    /** \brief Sleep for given milliseconds.
     *
     * \param millis time to sleep.
     */
    void sleep_milliseconds( uint32_t millis );
    /** \brief Waste time (and CPU) for given milliseconds.
     *
     * \param millis time to stay busy.
     */
    void busy_wait_milliseconds( uint32_t millis );
    /** \brief Set highest priority for timecritical measurements.
     */
    void set_max_priority();
    /** \brief Set the default priority.
     */
    void set_default_priority();

    HPCValue m_Hum; /**< humidity */
    HPCValue m_Temp; /**< temperature */
};

/** \brief Class to measure the humidity of the DHT22-sensor.
 */
class HPCHumidity22 : public HPCDHT
{
    Q_OBJECT
public:
    /** \brief Constructor
     *
     * \param name name of the sensor.
     * \param id database id of the sensor.
     * \param pin Inputpin.
     */
    explicit HPCHumidity22( const QString& name, quint32 id, quint16 pin )
        : HPCDHT(TSensorType::HPCHumidity22,name,id,pin) {}
    /** \brief Destructor.
     */
    virtual ~HPCHumidity22() {}

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();
};

/** \brief Class to measure the temperature of the DHT22-sensor.
 */
class HPCTemperature22 : public HPCDHT
{
    Q_OBJECT
public:
    /** \brief Constructor
     *
     * \param name name of the sensor.
     * \param id database id of the sensor.
     * \param pin Inputpin.
     */
    explicit HPCTemperature22( const QString& name, quint32 id, quint16 pin )
        :HPCDHT(TSensorType::HPCTemperature22,name,id,pin) {}
    /** \brief Destructor.
     */
    virtual ~HPCTemperature22() {}

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();
};

#endif // HPCDHT_H
